﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Awaken.MessageProvider
{
	/// <summary>
	/// 枚举对象帮助类
	/// <para>--------------------------</para>
	/// <para>enum Colors { Red, Green, Blue, Yellow };</para> 
	/// <para>1 Enum -> String</para>
	/// <para> i: Enum.ToString(format) 方法 Colors.Red.ToString("g") 结果：Red</para>
	/// <para> format 参数中可包含格式字符串“G”或“g”、“D”或“d”、“X”或“x”、“F”或“f”。</para>
	/// <para> 如果 format 是 null 或空字符串("")，则使用一般格式说明符("G")</para>
	/// <para> http://msdn.microsoft.com/zh-cn/library/a0h36syw(v=vs.110).aspx</para>
	/// <para> ii:GetName(typeof(Colors),1) 或 GetName(typeof(Colors),Clors.Red)  结果：Red</para>
	/// <para>2 String -> Enum</para>
	/// <para> (Colors)Enum.Parse(typeof(Colors),"Red");</para>
	/// <para>3 Int -> Enum</para>
	/// <para> Colors color=(Colors)2 </para>
	/// <para> Colors color=(Colors)Enum.ToObject(typeof(Colors),2) </para>
	/// </summary>
	public static class EnumHelper
    {
        /// <summary>
        /// 根据对象代码获取枚举值
        /// <para>enum Colors { Red } ,"Red" is a name</para> 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T GetValue<T>(string name) where T:struct
        {
            return (T)Enum.Parse(typeof (T), name,true);
        }
    }
}
